package com.xinan.hygl;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xinan.utils.Constants;
import com.xinan.utils.Pinyin;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HyglController extends BaseController {

	public void index() {
		render("/WEB-INF/views/hygl/hygl.html");
	}

	/**
	 * add
	 */
	public void newModes() {
		String id = getPara("id");
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
		render("/WEB-INF/views/hygl/hygl_form.html");
	}

	public void view() {
		String id = getPara("id");
		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
		render("/WEB-INF/views/hygl/view.html");
	}

	/**
	 * 在增加了SearchSql拦截之后就可以根据页面配置信息
	 * 自动装配查询语句放入request中,通过getAttr(Const.SEARCH_SQL)函数获得 根据实际需要自动组装到Sql语句里面
	 */
	@Before(SearchSql.class)
	public void query() {
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("rows");
		String where = getAttr(Const.SEARCH_SQL);
		Page<Hygl> page = Hygl.dao.page(pageNumber, pageSize, where);
		Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
		//renderJson(root);
	}

	public void hykgs() {
		render("/WEB-INF/views/hygl/hykgs.html");

	}
	@Before(SearchSql.class)

	public void gethy() {
//		String where = getAttr(Const.SEARCH_SQL);
		String where = "";
		String condition = getPara("search_EQ_card");
		where = "card='"+condition+"' or yhm='"+condition+"' or dh='"+condition
				+"' or cph='"+condition+"' or cph2='"+condition+"' or yhmjp='"+condition.toLowerCase()+"'";
		List<Record> newlist = Hygl.dao.findAlls(where);
		setAttr("newlist", newlist);

		render("/WEB-INF/views/hygl/hyglview.html");

	}
	
	/**
	 * save
	 * 
	 * @param
	 * @return
	 */
	public void save() {
		String bkry = getAttr(Constants.USER_NAME);
		String bkrq = "";
		Date dt = new Date();
		String card = getPara("card");
		String yhm = getPara("yhm");
		String dh = getPara("dh");
		String yhmjp = Pinyin.converterToFirstPinyin(yhm);
		List<Record> newlist = Hygl.dao.findAlls("       card=  '" + card + "'");
		List<Record> newlist2 = Hygl.dao.findAlls("        dh=   '"+ dh +"'");
		if(newlist.size()>0){
			//renderJson("卡号重复！请重新输入");
			render(new JsonRender("卡号重复！请重新输入").forIE());

		}
		else if(newlist2.size()>0){
			render(new JsonRender("该手机号码已绑定！请重新输入").forIE());
		}
		else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			bkrq = sdf.format(dt);
			if (getModel(Hygl.class).set("bkry", bkry).set("bkrq", bkrq).set("yhmjp", yhmjp).save()) {
				//renderJson(Constants.SAVESUCCESS);
				render(new JsonRender(Constants.SAVESUCCESS).forIE());
			} else {
				//renderJson(Constants.SAVEFAILED);
				render(new JsonRender(Constants.SAVEFAILED).forIE());

			}
		}
		
	
	}

	/**
	 * Post请求的菜单更新函数,可以接受菜单的id和菜单对象,并且根据菜单id进行更新,返回影响行数根据此判断是否操作成功
	 * 本例子使用rose框架的est风格url可以吧参数作为路径一部分进行传递
	 * 
	 * @param
	 * @return
	 */
	public void update() {
		String id= getPara("id");
		String dh = getPara("dh");
		List<Record> newlist2 = Hygl.dao.findAlls("       dh=  '" + dh + "'   and  id != '"+id+"'");
		if(newlist2.size()>0){
			render(new JsonRender("该手机号码已绑定！请重新输入").forIE());
		}else{
			if (getModel(Hygl.class).update()) {
				render(new JsonRender(Constants.EDITSUCCESS).forIE());

			//	renderJson(Constants.EDITSUCCESS);
			} else {
				render(new JsonRender(Constants.EDITFAILED).forIE());

			//	renderJson(Constants.EDITFAILED);
			}
		}
		
	}
	
	public void updatehyk(){
		String card=getPara("cards");
		String id= getPara("id");
		String dh = getPara("dh");
		List<Record> newlist = Hygl.dao.findAlls("       card=  '" + card + "'   and  id != '"+id+"'");
		List<Record> newlist2 = Hygl.dao.findAlls("       dh=  '" + dh + "'   and  id != '"+id+"'");
		if(newlist.size()>0){
			//renderJson("卡号重复！请重新输入");
			render(new JsonRender("卡号重复！请重新输入").forIE());

		}
		else if(newlist2.size()>0){
			render(new JsonRender("该手机号码已绑定！请重新输入").forIE());
		}
		else {
	
		if (getModel(Hygl.class).set("card",card).update()) {
			render(new JsonRender("1").forIE());
		//	renderJson("1");
		} else {
	//		renderJson("0");
			render(new JsonRender("0").forIE());

		}	
		}
	}
	/**
	 * Post请求的菜单删除函数,可以接受菜单ID 并且根据ID进行物理删除,并且返回影响行数,据此判断是否操作成功
	 * 
	 * @param
	 * @return
	 */
	public void delete() {
		if (Hygl.dao.deleteById(getPara("id"))) {
			render(new JsonRender(Constants.DELETESUCCESS).forIE());

	//		renderJson(Constants.DELETESUCCESS);
		} else {
			render(new JsonRender(Constants.DELETEFAILED).forIE());

		//	renderJson(Constants.DELETEFAILED);
		}
	}
}
